<?php
namespace app\controllers;

use app\models\GoodsRecorder;
use app\models\MemberRecorder;
use app\models\OrderRecorder;
use app\models\RateRecorder;
use app\models\RateuseRecorder;
use app\models\User;

class RateController extends BaseController{
    public function actionList(){
        $post = \Yii::$app->request->post('page');
        $page = intval($post['page'])-1;
        $pagesize = intval($post['pagesize']);
        $offset = $page*$pagesize;
        $list = RateRecorder::find();
        $count = $list->count();
        $list = $list->limit($pagesize)->offset($offset)->orderBy('addtime DESC')->asArray()->all();
        foreach($list as $k=>$v){
            $model = GoodsRecorder::find()->select(['goods_name'])->where('id='.$v['goods_id'])->one();
            $list[$k]['goods_name'] = empty($model)?"未知商品":$model->goods_name;
            $model = MemberRecorder::find()->where('id='.$v['user_id'])->one();
            $arr['user_pic'] = empty($model->user_pic)?\Yii::$app->params['non_pic']:$model->user_pic;
            $arr['user_pic'] = \Yii::$app->params['base_url'].$arr['user_pic'];
            $arr['user_name'] = empty($model->user_name)?"未知":$model->user_name;
            $arr['id'] = $v['user_id'];
            $list[$k]['user'] = $arr;
            $v['pics'] = json_decode($v['pics'],true);
            $pic_arr=[];
            foreach($v['pics'] as $pv){
                $pic_arr[]=str_replace("../",\Yii::$app->params['base_url'],$pv);
            }
            $list[$k]['pics'] = $pic_arr;
            $list[$k]['thumb_pic'] = empty($pic_arr[0])?'':$pic_arr[0];
            $list[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
            $list[$k]['rate'] = \Yii::$app->params['rate_list'][$v['rate']];
            $use_hits = RateuseRecorder::find()->where('rate_id='.$v['id'])->count();
            $use_hits = ($use_hits>=100)?"99+":$use_hits;
            $list[$k]['use_hits'] = $use_hits;
        }
        $result = ['result'=>1,'data'=>$list,'count'=>$count];
        return json_encode($result,JSON_NUMERIC_CHECK);
    }

    public function actionAddhf(){
        $post = \Yii::$app->request->post('form');
        $id = intval($post['id']);
        $model = RateRecorder::find()->where('id=:id',[':id' => $id])->one();
        if(!$model){
            $result =['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $model->hf_content = $post['hf_content'];
        $model->save(false);
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }

    public function actionDel(){
        $id = \Yii::$app->request->post('id');
        $model = RateRecorder::find()->where('id=:id',[':id' => $id])->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $model->delete();
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }

    public function actionDelpic(){
        $pic = \Yii::$app->request->post('pic');
        if(!empty($pic)){
            $pic = str_replace(\Yii::$app->params['base_url'],'../',$pic);
            @unlink($pic);
        }
        $result = ['result'=>1];
        return json_encode($result);
    }

    public function actionAdd(){
        $post = \Yii::$app->request->post('form');
        $goods_id = intval($post['goods_id']);
        if(empty($goods_id)){
            $result = ['result'=>0,'data'=>'请填写商品ID'];
            return json_encode($result);
        }
        $is = GoodsRecorder::find()->where('id=:goods_id',[':goods_id' => $goods_id])->count();
        if(!$is){
            $result = ['result'=>0,'data'=>'不存在相关商品，请重新操作'];
            return json_encode($result);
        }
        $rate = intval($post['rate']);
        if($rate<0){
            $result = ['result'=>0,'data'=>'请选择评价'];
            return json_encode($result);
        }
        $pics = $post['pics'];
        $pics = is_array($pics)?$pics:[];
        $arr=[];
        foreach($pics as $pv){
            $arr[]=str_replace(\Yii::$app->params['base_url'],'../',$pv);
        }

        $content = $post['content'];
        $model = new RateRecorder();
        $model->goods_id = $goods_id;
        $model->rate = $rate;
        $model->pics = json_encode($arr);
        $model->content = $content;
        $model->addtime = time();
        $model->save(false);
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }


}